%% Kostaki Ioanna
%% AEM 837

%% orthogwnioi pinakes
%% Askisi 1
disp('ORTHOGWNIOI PINAKES');

a = [2 1 1]
b = [1 -2 0]

eswt_gin=a*b'
%to eswt_gin=0 ara a,b einai orthogwnia
%den einai to monadiko to b giati estw b=[b1 b2 b3]
%a*b'=2b1 + b2 + b3
%paratirw oti exw 1 exiswsi gia 3 agnwstous ara 2 vathmous eleuferias opote
%apeires liseis.

%% Askisi 2

a = [2 1 1]'; 
b = [1 2 -4]'; 
c = cross(a,b); 
figure(1); 
clf; 
drawVector([a b c], 'r.', {'a','b','c'}); 
view(-160, 7)
pause;

%% Askisi 3

A = [a b c];
X = sampleUnitSphere(3,200); 
AX = A*X; 
figure(1); clf; hold on; 
plot3( X(1,:), X(2,:), X(3,:),'b.'); view(-60,10)
plot3(AX(1,:),AX(2,:),AX(3,:),'g.'); 
drawVector(A, 'r.') 
hold off; axis square equal; grid on;
pause;

%% Askisi 4

B = [a b c]; 
C = diag([1/norm(a) 1/norm(b) 1/norm(c)]); 
A = B*C;
X = sampleUnitSphere(3,200); 
AX = A*X; 
figure(1); clf; hold on; 
plot3( X(1,:), X(2,:), X(3,:),'b.'); view(-60,10)
plot3(AX(1,:),AX(2,:),AX(3,:),'g.'); 
drawVector(A, 'r.') 
hold off; axis square equal; grid on;
pause;

A = A*B
X = sampleUnitSphere(3,200); 
AX = A*X; 
figure(1); clf; hold on; 
plot3( X(1,:), X(2,:), X(3,:),'b.'); view(-60,10)
plot3(AX(1,:),AX(2,:),AX(3,:),'g.'); 
drawVector(A, 'r.') 
hold off; axis square equal; grid on;
pause;

A =A*A*B
X = sampleUnitSphere(3,200); 
AX = A*X; 
figure(1); clf; hold on; 
plot3( X(1,:), X(2,:), X(3,:),'b.'); view(-60,10)
plot3(AX(1,:),AX(2,:),AX(3,:),'g.'); 
drawVector(A, 'r.') 
hold off; axis square equal; grid on;
pause;

A =A*A*A*B
X = sampleUnitSphere(3,200); 
AX = A*X; 
figure(1); clf; hold on; 
plot3( X(1,:), X(2,:), X(3,:),'b.'); view(-60,10)
plot3(AX(1,:),AX(2,:),AX(3,:),'g.'); 
drawVector(A, 'r.') 
hold off; axis square equal; grid on;
pause;



%% NORMES
disp('NORMES');
%% Askisi 1

B = [1 3; 1 1];
C = reshape(1:4,2,2);
A = [-2 3; -3 1 ]; 
X = sampleUnitSphere(2, 100); 
AX = A*X;
BX = B*X;
CX = C*X;
figure(1); clf; hold on; 
plot( X(1,:), X(2,:),'b.'); 
plot(AX(1,:),AX(2,:),'r.'); 
plot(BX(1,:),BX(2,:),'y.');  
plot(CX(1,:),CX(2,:),'g.'); 


%% Askisi 2

% o -B tha exei ola ta stoixeia tou B me to aditheto prosimo
% o B' tha einai o anastrofos tou B diladi oi stiles tou B tha einai oi
% grammes tou B'

B = [1 3; 1 1]
-B
B'

%% Askisi 3

A = [-2 3; -3 1 ]; 
X = sampleUnitSphere(2, 100); 
for count = 1:1:6,
    AX= (A^count)*X; 
    figure(1); 
clf; hold on; 
plot( X(1,:), X(2,:),'b.'); 
plot(AX(1,:),AX(2,:),'r.'); 
pause;
end

%% Askisi 4

A = [1 3 -2; 8 0 1; 2 2 6 ];
B = reshape(1:9,3,3);
%Boroume poly eykola na vroume tis taksis ton pinakwn mesw toy matlab me
%tin entoli rank(table_name)
disp('H taksi toy A einai ');
rank(A)
disp('H taksi toy B einai ');
rank(B)

x = [-3 2 1]'; 
figure(1); clf; hold on; 
%drawVector([ x [A*x; 0] ], {'x', 'Ax'});

%% Askisi 5

A = diag([1 2 3]);
n = norm(A,2)



disp('PROVOLES');

%% Askisi 1

A = [1 0; 0 1; 1 0];
B = [1 2; 0 1; 1 0];

a = [4 2 3]';  
figure(1); clf; hold on;
drawVector(a, 'r.', {'a'}); 
P_orth = A*A';
a_orth = P_orth *a;
drawVector(a_orth , 'b.', 'a_{orth}')
drawSpan(A) 
hold off 
view(150, 15)
pause;

a = [4 2 3]'; 
figure(1); clf; hold on;
drawVector(a, 'r.', {'a'}); 
P_orth = B*B';
a_orth = P_orth *a;
drawVector(a_orth, 'b.', 'a_{orth}')
drawSpan(B) 
hold off 
view(150, 15)
pause;

%% Askisi 2
n = [-.5 6 3]';
figure(1); clf; hold on;
drawVector(a, 'r.', {'a'}); 
P_oblique = A*[1 0 0;0 1 0]*inv([A n]);
a_oblique = P_oblique*a;  
drawVector(a_oblique, 'b.', 'a_{oblique}')
drawSpan(A) 
hold off 
view(150, 15)
pause;

figure(1); clf; hold on;
drawVector(a, 'r.', {'a'}); 
P_oblique = B*[1 0 0;0 1 0]*inv([B n]);
a_oblique = P_oblique*a;  
drawVector( a_oblique, 'b.', 'a_{oblique}')
drawSpan(B) 
hold off 
view(150, 15)
pause;


%% Askisi 2

disp('ADISTROFOS PINAKAS');

e1 = [1;0]; 
e2 = [0;1]; 
figure(1); clf; hold on; 
drawVector(A, {'a','b'}); 
drawVector([e1 e2], 'gs', {'e_1', 'e_2'}) 
hold off;

A = [1 1; -2 2]
invA = inv(A)
P1 = A*diag(invA(:,1)); 
P2 = A*diag(invA(:,2)); 
figure(1); hold on; 
drawVector(P1, 'rd')
drawVector(P2, 'ms') 
drawLine([e1 P1(:,1)]); 
drawLine([e1 P1(:,2)]); 
drawLine([e2 P2(:,1)]);
drawLine([e2 P2(:,2)]);
hold off



%% Askisi 1

disp('PARAGODOPOIHSH LU');

A = [2 4; 3 6];
b = [4 7]';

x = A\b;
figure(2); clf; hold on; 
drawVector(A, {'a_1', 'a_2'}) 
drawVector(b, 'rs', {'b'});
Pb = A*diag(x);
drawVector(Pb, 'g+', {'x_{11}\cdot a_1', 'x_{21}\cdot a_2'});
drawLine([b Pb(:,1)]);
drawLine([b Pb(:,2)])
;hold off; 
title('Ax=b in the column space', 'FontSize', 14);

figure(3); 
clf; hold on; 
drawPlane(A(1,:), b(1), 'b');
drawPlane(A(2,:), b(2), 'g'); 
hold off title('Ax=b in the row space', 'FontSize', 14);

%% Askisi 2

A = [3 12 1; 12 0 2; 0 2 3]; 
b = [2.36 5.26 2.77]';
x = A\b;
Pb = A*diag(x);
figure(1);clf;hold on;
drawVector(A, {'a_1','a_2','a_3'});
drawVector(b, 'rs', {'b'}); 
drawLine([b Pb(:,1)]);
hold off;
title('Ax=b in the column space', 'FontSize', 14);
figure(3); clf; hold on;
drawPlane(A(1,:), b(1), 'b');
drawPlane(A(2,:), b(2), 'g');
drawPlane(A(3,:), b(3), 'r');
plot3(x(1),x(2),x(3), 'mx', 'LineWidth', 4, 'MarkerSize', 15) 
hold off 
title('Ax=b in the row space', 'FontSize', 14);

%% Askisi 3
A = [ 4 1 ; 0 3.5 ]; 
b = [2 -3]'; 
figure(3); clf; hold on;
drawPlane(A(1,:), b(1), 'b');
drawPlane(A(2,:), b(2), 'g');
hold off
title('Ax=b in the row space', 'FontSize', 14); 
figure(3);
hold on;



%% Askisi 4

U = [ 2 1 1 0; 0 1 1 1; 0 0 2 2; 0 0 0 2 ];
I=eye(4);
for j=1:4 b=I(1:4,j);
    x(1:4,j)=backSub(U,b);
end
V=Ux;
display(x); 
display(V);


%% Askisi 5

A = [ 2 1 1 0; 4 3 3 1; 8 7 9 5; 6 7 9 8 ];
[L, U] = slu(A,1);